﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class FrmTachBan : Form
    {
        ChiTietHoaDonBUS ctBUS = new ChiTietHoaDonBUS();
        public FrmTachBan()
        {
            InitializeComponent();
        }

        private void LoadDSban()
        {
            BanBUS banBUS = new BanBUS();
            cbxbancantach.DataSource = banBUS.laydanhsachbantheotrangthai("Bận");
            cbxbancantach.ValueMember = "MaBan";
            cbxbantachtoi.DataSource = banBUS.laydanhsachbantheotrangthai("Trống");
            cbxbantachtoi.ValueMember = "MaBan";
        }

        private void FrmTachBan_Load(object sender, EventArgs e)
        {

            LoadDSban();

            int mahd = ctBUS.LayMaHDChiTietHoaDonDam(cbxbancantach.SelectedValue.ToString());
            List<ChiTietHoaDonDTO> ctdto = ctBUS.LayDanhSachChiTietHoaDon(mahd);
            foreach (ChiTietHoaDonDTO ctrow in ctdto)
            {
                string mahdghi = ctrow.MaHD.ToString();
                string mamonan = ctrow.MaMonAn.ToString();
                string maban = ctrow.MaBan.ToString();
                string soluong = ctrow.SoLuong.ToString();
                string dongia = ctrow.DonGia.ToString();
                string tongtien = ctrow.TongTien.ToString();
                dtgvMonBanCanTach.DataSource = null;
                dtgvMonBanCanTach.Rows.Add(null,mahdghi, mamonan ,maban,soluong,dongia,tongtien);
            }
            
        }

        private void cbxbancantach_SelectedIndexChanged(object sender, EventArgs e)
        {
            dtgvMonBanTachToi.Rows.Clear();
            dtgvMonBanCanTach.Rows.Clear();
            int mahd = ctBUS.LayMaHDChiTietHoaDonDam(cbxbancantach.SelectedValue.ToString());
            List<ChiTietHoaDonDTO> ctdto = ctBUS.LayDanhSachChiTietHoaDon(mahd);
            foreach (ChiTietHoaDonDTO ctrow in ctdto)
            {
                string mahdghi = ctrow.MaHD.ToString();
                string mamonan = ctrow.MaMonAn.ToString();
                string maban = ctrow.MaBan.ToString();
                string soluong = ctrow.SoLuong.ToString();
                string dongia = ctrow.DonGia.ToString();
                string tongtien = ctrow.TongTien.ToString();
                dtgvMonBanCanTach.DataSource = null;
                dtgvMonBanCanTach.Rows.Add(null, mahdghi, mamonan, maban, soluong, dongia, tongtien);
            }
            
        }

        private void FrmTachBan_FormClosed(object sender, FormClosedEventArgs e)
        {
            Program.frm_manhinhchinh.sub_mn_TachBan.Checked = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int flag = 0;
            for (int i = dtgvMonBanCanTach.Rows.Count-1; i >= 0;i-- )
            {
                if (dtgvMonBanCanTach.Rows[i].Cells[0].Value != null)
                {
                    flag = 1;
                    string mahdghi = dtgvMonBanCanTach.Rows[i].Cells[1].Value.ToString();
                    string mamonan = dtgvMonBanCanTach.Rows[i].Cells[2].Value.ToString();
                    string maban = dtgvMonBanCanTach.Rows[i].Cells[3].Value.ToString();
                    string soluong = dtgvMonBanCanTach.Rows[i].Cells[4].Value.ToString();
                    string dongia = dtgvMonBanCanTach.Rows[i].Cells[5].Value.ToString();
                    string tongtien = dtgvMonBanCanTach.Rows[i].Cells[6].Value.ToString();
                    dtgvMonBanTachToi.Rows.Add(mahdghi, mamonan,maban, soluong, dongia, tongtien);
                    dtgvMonBanCanTach.Rows.RemoveAt(i);
                }
            }
            if (flag == 0)
            {
                MessageBox.Show("Chưa chọn món ăn để tách !");
                return;
            }
        }

        private void btnChonLai_Click(object sender, EventArgs e)
        {
            cbxbancantach_SelectedIndexChanged(sender, e);
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThucHien_Click(object sender, EventArgs e)
        {
            if (dtgvMonBanTachToi.Rows.Count == 0)
            {
                MessageBox.Show("Bạn chưa chuyển món để tách bàn !!!", "Thông báo");
                return;
            }
            if (dtgvMonBanCanTach.Rows.Count == 0)
            {
                MessageBox.Show("Bạn không thể chuyển hết món qua bàn khác \n \t  Vui lòng chọn lại !!!", "Thông báo");
                return;
            }
            List<ChiTietHoaDonDTO> cthddtomoi= new List<ChiTietHoaDonDTO>();
            List<ChiTietHoaDonDTO> cthddtocu = new List<ChiTietHoaDonDTO>();
            HoaDonDTO hd = new HoaDonDTO();
            HoaDonBUS hdBUS = new HoaDonBUS();
            hd.MaHD = hdBUS.LayMaHoaDon() + 1;
            hd.GioVao = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
            hd.GioRa = null;
            hd.ThanhTien = 0;
            hd.TenUser = User.TenUser;
            hd.TinhTrangHD = "Chưa tính tiền";
            hdBUS.ThemHoaDon(hd);
            for (int i = 0; i < dtgvMonBanTachToi.Rows.Count; i++)
            {
                ChiTietHoaDonDTO ctdtomoi = new ChiTietHoaDonDTO();
                ctdtomoi.MaHD = hd.MaHD ;
                ctdtomoi.MaMonAn = int.Parse(dtgvMonBanTachToi.Rows[i].Cells[1].Value.ToString());
                ctdtomoi.MaBan = cbxbantachtoi.SelectedValue.ToString();
                ctdtomoi.SoLuong = int.Parse(dtgvMonBanTachToi.Rows[i].Cells[3].Value.ToString());
                ctdtomoi.DonGia = float.Parse(dtgvMonBanTachToi.Rows[i].Cells[4].Value.ToString());
                ctdtomoi.TongTien = float.Parse(dtgvMonBanTachToi.Rows[i].Cells[5].Value.ToString());
                cthddtomoi.Add(ctdtomoi);
            }
            ChiTietHoaDonBUS ctbus = new ChiTietHoaDonBUS();
            foreach (ChiTietHoaDonDTO ctdto in cthddtomoi)
            {
                ctbus.ThemChiTietHoaDon(ctdto);//thêm cthd mới vào
            }
            hd.ThanhTien = ctBUS.TinhTongTienChiTietHoaDon(hd.MaHD);
            hdBUS.CapNhatHoaDon(hd);
            for (int i = 0; i < dtgvMonBanTachToi.Rows.Count; i++)
            {
                ChiTietHoaDonDTO ctdtocu = new ChiTietHoaDonDTO();
                ctdtocu.MaHD = int.Parse(dtgvMonBanCanTach.Rows[i].Cells[1].Value.ToString()); 
                ctdtocu.MaMonAn = int.Parse(dtgvMonBanTachToi.Rows[i].Cells[1].Value.ToString());
                ctdtocu.MaBan = cbxbancantach.SelectedValue.ToString();
                ctdtocu.SoLuong = int.Parse(dtgvMonBanTachToi.Rows[i].Cells[3].Value.ToString());
                ctdtocu.DonGia = float.Parse(dtgvMonBanTachToi.Rows[i].Cells[4].Value.ToString());
                ctdtocu.TongTien = float.Parse(dtgvMonBanTachToi.Rows[i].Cells[5].Value.ToString());
                cthddtocu.Add(ctdtocu);
            }
            foreach (ChiTietHoaDonDTO ctdto in cthddtocu)
            {
                ctbus.XoaChiTietHoaDon(ctdto);//xóa cthd cũ đi
            }
            hd.MaHD = int.Parse(dtgvMonBanCanTach.Rows[0].Cells[1].Value.ToString());
            hd.TenUser = User.TenUser;
            hd.TinhTrangHD = "";
            hd.GioRa = "";
            hd.ThanhTien = ctBUS.TinhTongTienChiTietHoaDon(hd.MaHD);
            hdBUS.CapNhatHoaDon(hd);

            //Cập nhật lại trạng thái bàn
            BanBUS banBUS = new BanBUS();
            BanDTO bandto = new BanDTO();
            bandto.MaBan = cbxbantachtoi.SelectedValue.ToString();
            bandto.TrangThai = "Bận";
            banBUS.CapNhatBan(bandto);
            MessageBox.Show("Tách bàn thành công !!!","Thông báo");
            LoadDSban();
            cbxbancantach_SelectedIndexChanged(sender, e);

        }
    }
}
